package com.jinzhu.favorite;

import java.util.HashMap;
import java.util.Map;

/**
 * https://blog.csdn.net/u013773608/article/details/95229311
 */
// Typesafe heterogeneous container pattern - API
public class Favorites {
    // Typesafe heterogeneous container pattern - implementation
    private Map<Class<?>, Object> favorites = new HashMap<Class<?>, Object>();

    public <T> void putFavorite(Class<T> type, T instance) {
        if (type == null) {
            throw new NullPointerException("Type is null");
        }
        favorites.put(type, instance);
    }
    //favorites.get(type)是获取map对应的值，type.cast是将值进行类型转换
    public <T> T getFavorite(Class<T> type) {
        return type.cast(favorites.get(type));
    }
}
